package servidorapjava.transmissao;

import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;
import javax.media.*;
import javax.media.control.FormatControl;
import javax.media.control.TrackControl;
import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;

public class TransmiteVideoAudio {
    
    
    
    public TransmiteVideoAudio() {
    }
    
    public boolean criaConfiguraProcessor(){
        Format formats[] = new Format[2];
        formats[0] = new AudioFormat(AudioFormat.IMA4);
        formats[1] = new VideoFormat(VideoFormat.CINEPAK);
        FileTypeDescriptor outputType =
                new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME);
        
        
        Processor p = null;
        try {
            p = Manager.createRealizedProcessor(new ProcessorModel(formats,
                    outputType));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (NoProcessorException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (CannotRealizeException e) {
            e.printStackTrace();
            System.exit(-1);
        }
// get the output of the processor
        DataSource source = p.getDataOutput();
// create a File protocol MediaLocator with the location
// of the file to
// which bits are to be written
        MediaLocator dest = new MediaLocator("file://foo.mov");
// create a datasink to do the file writing & open the
// sink to make sure
// we can write to it.
        DataSink filewriter = null;
        try {
            filewriter = Manager.createDataSink(source, dest);
            filewriter.open();
        } catch (NoDataSinkException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (SecurityException e) {
            e.printStackTrace();
            System.exit(-1);
        }
// now start the filewriter and processor
        try {
            filewriter.start();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        p.start();
        return true;
// stop and close the processor when done capturing...
// close the datasink when EndOfStream event is received...
        
    }
    
    public static boolean criaProcessorAudio(){
        Processor processor = null;
        // First find a capture device that will capture linear audio
        // data at 8bit 8Khz
        AudioFormat format= new AudioFormat(AudioFormat.LINEAR, 8000, 8, 1);
        Vector devices= CaptureDeviceManager.getDeviceList( format);
        CaptureDeviceInfo di= null;
        if (devices.size() > 0) {
            di = (CaptureDeviceInfo) devices.elementAt( 0);
        } else {
            // exit if we could not find the relevant capturedevice.
            System.exit(-1);
        }
        // Create a processor for this capturedevice & exit if we
        // cannot create it
        try {
            processor = Manager.createProcessor(di.getLocator());
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (NoProcessorException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        // configure the processor
        processor.configure();
        // block until it has been configured
        processor.setContentDescriptor(
                new ContentDescriptor( ContentDescriptor.RAW));
        TrackControl track[] = processor.getTrackControls();
        boolean encodingOk = false;
        // Go through the tracks and try to program one of them to
        // output gsm data.
        for (int i = 0; i < track.length; i++) {
            if (!encodingOk && track[i] instanceof FormatControl) {
                if (((FormatControl)track[i]).
                        setFormat( new AudioFormat(AudioFormat.GSM_RTP,
                        8000,
                        8,
                        1)) == null) {
                    track[i].setEnabled(false);
                } else {
                    encodingOk = true;
                }
            } else {
                // we could not set this track to gsm, so disable it
                track[i].setEnabled(false);
            }
        }
        // At this point, we have determined where we can send out
        // gsm data or not.
        // realize the processor
        if (encodingOk) {
            processor.realize();
            // block until realized.
            // get the output datasource of the processor and exit
            // if we fail
            DataSource ds = null;
            try {
                ds = processor.getDataOutput();
            } catch (NotRealizedError e) {
                e.printStackTrace();
                System.exit(-1);
            }
            // hand this datasource to manager for creating an RTP
            // datasink our RTP datasimnk will multicast the audio
            try {
                String url= "rtp://192.168.0.105:49150/audio/1";
                MediaLocator m = new MediaLocator(url);
                DataSink d = Manager.createDataSink(ds, m);
                d.open();
                d.start();
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(-1);
            }
        }
        return true;
    }
   
    public static void main(String[] args) {
        criaProcessorAudio();
    }
    
}
   
    
    
